<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <p style="display: flex;align-items: center;">
      所属系统：
      <Platform style="margin-right:10px;" @childfun="childfun" />
      <el-button @click="searchfun">搜索</el-button>
    </p>
    <div class="formDiv">
      <el-form ref="form"  :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item>
              <el-button
                v-if="hasPermission('AdminMenuProvider.saveMenu')"
                type="primary"
                @click="toRoutes('menuManageAdd')"
              >添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="menuName" label="菜单名称" />
        <el-table-column label="父级">
          <template slot-scope="scope">
            <div v-if="scope.row.parentMenuName">{{ scope.row.parentMenuName }}</div>
            <div v-else>顶级</div>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="菜单描述" />
        <el-table-column prop="sort" label="排序" />
        <el-table-column prop="url" label="url" />
        <el-table-column label="图标">
          <template slot-scope="scope">
            <div>
              <svg style="width:36px;height:36px" aria-hidden="true" class="svg-icon">
                <use :xlink:href="'#icon-'+scope.row.icon" />
              </svg>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              class="switchStyle"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
              @click.native="changeStatus(scope.row.id,'status',scope.row.status)"
            />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              v-if="hasPermission('AdminMenuProvider.updateMenu')"
              type="text"
              size="small"
              @click="toRoutes('menuManageEdit',scope.row.id)"
            >编辑</el-button>
            <el-button
              v-if="hasPermission('AdminMenuProvider.deleteMenu')"
              type="text"
              size="small"
              @click="deleteList(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.pageNum"
            :page-sizes="page_sizes"
          
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
// ,create,deleteFn

import { listAdminMenus, deleteMenu, updateMenuStatus } from "@/api/menuManage";
import Platform from "../components/platform";
export default {
  name: "MenuManageIndex",
  components: {
    Platform
  },
  filters: {},
  data() {
    return {
      val: "1",
      dates: "",
      dialogVisible: false,
      tableLoading: true,
      form: {
        pageNum: 1,
        pageSize: 10
      },
      addForm: {},
      tableData: [],
      roleOptions: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10
      }
    };
  },
  inject: ["reload"],
  mounted() {
    this.getList(this.listForm);
    this.roleOptions = window.helper.roleFn("listReturn");
  },
  methods: {
    // 搜索的
    childfun(val) {
      console.log(val);
      this.form.subSys = val === "" ? -1 : val;
    },
    searchfun() {
      this.form.pageNum = 1
      this.listForm.pageNum =1
      this.getList(this.form);
    },
    // 按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    // 上下架与状态修改
    changeStatus(userId, type, status) {
      const datas = {
        id: userId,
        status: status
      };

      updateMenuStatus(datas)
        .then(res => {
          console.log(res);
          if (res.data.code === 0) {
            this.$message({
              message: "修改成功",
              type: "success"
            });
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 删除
    deleteList(e) {
      const datas = { id: e };
      console.log(datas);
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteMenu(datas).then(res => {
            if (!res.data.code) {
              this.succStatus("删除");
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    // 路由跳转
    toRoutes(e, userId) {
      const route = { name: e, query: { userId: userId } };
      this.$router.push(route);
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    // 列表数据获取
    getList(e) {
      this.tableLoading = true;
      const datas = e;
      listAdminMenus(datas)
        .then(res => {
          if (!res.data.code) {
            this.tableData = res.data.data.menuDTOS;
            this.page_totals = res.data.data.count;
            console.log(this.tableData);
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getList(this.form);
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
